﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Content;

namespace POB
{
    class Player
    {
        #region Game Consts
        const int MaxSlots = 5;
        #endregion Game Consts

        public float Energie { get; set; }
        public Vector2 Position { get; set; }
        public Vector2 Direction { get; set; }

        private PlayerIndex PlayerIndex { get; set; }
        public Slot[] Slots { get; set; }
        private CircleMenu menu;

        public List<UnitInfo> AllUnits { get; set; }
        public World World { get; set; }

        public Player(Vector2 position, Vector2 direction, XElement XUnits, World world)
        {
            this.World = world;
            this.Direction = direction;
            this.Position = position;
            this.AllUnits = new List<UnitInfo>();
            this.Slots = new Slot[MaxSlots];

            for(int i = 0; i < MaxSlots; i++)
            {
                this.Slots[i] = new Slot(this);
            }

            foreach(XElement Xunit in XUnits.Elements())
            {
                UnitInfo info = new UnitInfo(Xunit, world, this);
                this.AllUnits.Add(info);
            }

            this.menu = new CircleMenu()
            {
                Items = AllUnits.ToList<IMenuItem>(),
            };
        }

        public void Update(float elapsedTime)
        {
            foreach(Slot slot in this.Slots)
                slot.Update(elapsedTime);
        }

        public void HandleInput(InputState input)
        {
            if(input.CurrentGamePadStates[(int)PlayerIndex].Triggers.Left > .5f)
            {
                if(input.LastGamePadStates[(int)PlayerIndex].Triggers.Left <= .5f)
                    this.menu.NavigateBack(true);
                this.menu.Active = true;
            }
            else
            {
                this.menu.Active = false;
            }

            this.menu.Input(input, PlayerIndex);
        }

        public void Draw(SpriteBatch batch, Matrix matrix)
        {
            for(int i = 0; i < Slots.Length; i++)
            {
                Slots[i].Draw(batch, Matrix.CreateTranslation(60f + 300f * i, 950, 0f) * matrix);
            }

            menu.Draw(batch, Matrix.CreateTranslation(540, 500, 0) * matrix, 300f);
        }
    }
}
